package com.xm.xmapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
import com.xm.xmapp.R;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.dialog.TipLeftRightDialog;
import com.xm.xmapp.http.NetWorkData;
import com.xm.xmapp.http.UpdataAppClass;
import com.xm.xmapp.req.ReqVersion;
import com.xm.xmapp.resp.RespJpushNotifaction;
import com.xm.xmapp.resp.RespVersion;
import com.xm.xmapp.tool.AndroidTool;
import com.xm.xmapp.tool.Helper_SharedPreferences;

import java.io.File;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by LDW on 2017/7/19.
 */
public class StartActivity extends BaseActivity  {

    private static final int REQ_VERSION = 1;
    @Bind(R.id.lin)
    LinearLayout ll_layout;
    private TipLeftRightDialog mydialog;
    private RespVersion.VersionInfoBean t;
    private RespJpushNotifaction mJpushNotifactionResponse;// 极光推送，后台打开app，主页弹出dialog
    /**
     * 是否打开过引导页
     */
    public static final String YES_OR_NO = "yesORno";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
    }

    @Override
    public void initView(Bundle savedInstanceState) {

        ReqVersion requestBean = new ReqVersion();
        requestBean.app_version_number = AndroidTool.getVersionCode();
        requestBean.phone_model = AndroidTool.getMODEL();

        mNetWorkData = new NetWorkData() {
            @Override
            public void dissMissDialog(int code) {

            }

            @Override
            public void onResponse(int code, String response) {

                RespVersion bean = JSON.parseObject(response, RespVersion.class);
                UpdataAppClass.getUpdataAppClass().setQuxiao(false);
                if (bean.getIs_version_update() == 1) {
                    t = bean.getVersion_info();
                    UpdataAppClass.APK_NAME = "yibuyongche_" + t.getApp_show_version_number();
                    File file = new File(MyAppcation.getMyAppcation().getExternalFilesDir("myapk") + "/",
                            UpdataAppClass.APK_NAME + ".apk");
                    if (!file.exists()) {
                        if (bean.getVersion_info().getIs_force_update().equals("1")) {
                            showBuilder("有新版本", "文件大小:" + t.getApp_file_size() + "\n" + t.getApp_version_description(), 1);
                        } else {
                            showBuilder("有新版本", "文件大小:" + t.getApp_file_size() + "\n" + t.getApp_version_description(), 1);
                        }
                    } else {
                        UpdataAppClass.getUpdataAppClass().update();
                        UpdataAppClass.getUpdataAppClass().update();

                    }

                } else {
                    begin();
                }
            }

            @Override
            public void onErrorResponse(int code, String errorResponse) {

            }
        };

        getNetWorkData(requestBean, REQ_VERSION);
    }

    protected void showBuilder(String title, String message, int flag) {

        mydialog = new TipLeftRightDialog(this, title, message, flag);
        mydialog.setCancelable(false);
        mydialog.setDialogInterFace("取消", "下载", new TipLeftRightDialog.DialogAble() {

            @Override
            public void right(int flag, Object data) {

                UpdataAppClass.getUpdataAppClass().downFile(t.getUrl(), StartActivity.this, new UpdataAppClass.HandlerInterFace() {

                    @Override
                    public void run(Message msg) {
                        finish();
                    }
                });
                mydialog.dismiss();
            }

            @Override
            public void left(int flag, Object data) {

                if (t != null && t.getIs_force_update().equals("1")) {
                    finish();
                } else {
                    begin();
                }
                mydialog.dismiss();
            }
        }).show();

    }

    private void begin() {
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                startToNext();
            }
        }, 1000);
    }

    /**
     * 跳转
     */
    protected void startToNext() {

        if (Helper_SharedPreferences.get_bool_sp(YES_OR_NO)) {
            intent = new Intent(MyAppcation.getMyAppcation(), MainActivity.class);
            intent.putExtra("bean", mJpushNotifactionResponse);
            startActivity(intent);
            overridePendingTransition(R.anim.welcome_activity_in, R.anim.welcome_activity_out);
            Helper_SharedPreferences.set_bool_sp(YES_OR_NO, true);
        } else {
            intent = new Intent(MyAppcation.getMyAppcation(), GuideActivity.class);
            startActivity(intent);
        }
        finish();
    }


    @OnClick(R.id.lin)
    public void onClick() {
        startToNext();
    }
}
